package com.haitaoit.chengquan.module.home.activity.zhaopin;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.home.activity.CopyPopup;
import com.haitaoit.chengquan.module.home.model.RecruitCompanyModel;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.CircleImageView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RecruitCompanyActivity extends BaseActivity {
    @ViewInject(R.id.recruit_com_sc1)
    ScrollView      recruit_com_sc1;
    @ViewInject(R.id.recruit_com_sc2)
    ScrollView      recruit_com_sc2;
    @ViewInject(R.id.recruit_title)
    TextView        recruit_title;
    @ViewInject(R.id.recruit_time)
    TextView        recruit_time;
    @ViewInject(R.id.recruit_name)
    TextView        recruit_name;
    @ViewInject(R.id.post_salary)
    TextView        post_salary;
    @ViewInject(R.id.recruit_area)
    TextView        recruit_area;
    @ViewInject(R.id.recruit_degree_required)
    TextView        recruit_degree_required;
    @ViewInject(R.id.experience_requirements)
    TextView        experience_requirements;
    @ViewInject(R.id.recruit_work)
    TextView        recruit_work;
    @ViewInject(R.id.recruit_contact)
    TextView        recruit_contact;
    @ViewInject(R.id.contact_phone)
    TextView        contact_phone;
    @ViewInject(R.id.job_description)
    TextView        job_description;
    @ViewInject(R.id.tv_zhiwei_leibie)
    TextView        tv_zhiwei_leibie;
    @ViewInject(R.id.center_img)
    CircleImageView center_img;
    @ViewInject(R.id.recruit_name2)
    TextView        recruit_name2;
    @ViewInject(R.id.recruit_address)
    TextView        recruit_address;
    @ViewInject(R.id.company_introduction)
    TextView        company_introduction;
    @ViewInject(R.id.recruit_work_address)
    TextView        recruit_work_address;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recruit_company);
        ViewUtils.inject(this);

        recruit_com_sc2.setVisibility(View.GONE);
        httpGetrecruitment_information_model(getIntent().getStringExtra("id"));
    }

    @OnClick({R.id.left, R.id.recruit_com_rb1, R.id.recruit_com_rb2, R.id.contact_phone, R.id.recruit_work_address,})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left:
                finish();
                break;
            case R.id.recruit_com_rb1:
                recruit_com_sc1.setVisibility(View.VISIBLE);
                recruit_com_sc2.setVisibility(View.GONE);
                break;
            case R.id.recruit_com_rb2:
                recruit_com_sc1.setVisibility(View.GONE);
                recruit_com_sc2.setVisibility(View.VISIBLE);
                break;
            case R.id.contact_phone:// 电话
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + contact_phone.getText().toString().substring(3))));
                break;
            case R.id.recruit_work_address:// 地址点击
                if(copyPopup==null)
                    copyPopup=new CopyPopup(RecruitCompanyActivity.this);
                copyPopup.show(recruit_work_address,recruit_work_address.getText().toString().trim());
                break;
        }
    }

    private CopyPopup copyPopup;

    private void httpGetrecruitment_information_model(String id) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("id", id));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("id", id);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getrecruitment_information_model, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    Gson gson = new Gson();
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            RecruitCompanyModel model = gson.fromJson(Response, RecruitCompanyModel.class);
                            recruit_title.setText(model.getRecruitment_information_model().getTitle());
                            recruit_time.setText(model.getRecruitment_information_model().getAddtime());
                            recruit_name.setText(model.getRecruitment_information_model().getCompanyname());
                            post_salary.setText(model.getRecruitment_information_model().getPost_salary());
                            recruit_area.setText(model.getRecruitment_information_model().getArea());
                            recruit_degree_required.setText("最高学历：" + model.getRecruitment_information_model().getDegree_required());
                            experience_requirements.setText("经验要求：" + model.getRecruitment_information_model().getExperience_requirements());
//                            recruit_work.setText("上班地点：" + model.getRecruitment_information_model().getWork());
                            recruit_work_address.setText(model.getRecruitment_information_model().getWork());
                            recruit_contact.setText("联系人：" + model.getRecruitment_information_model().getContact());
                            contact_phone.setText("电话：" + model.getRecruitment_information_model().getContact_phone());
                            job_description.setText(model.getRecruitment_information_model().getJob_description());
                            tv_zhiwei_leibie.setText(model.getRecruitment_information_model().getJob_category());

                            ImageLoader.getInstance().displayImage(model.getCompany_model().getHead_portrait(), center_img);
                            recruit_name2.setText(model.getCompany_model().getName());
                            recruit_address.setText(getString(R.string.RecruitCompany_con_address)+model.getCompany_model().getArea());
                            company_introduction.setText(model.getCompany_model().getCompany_introduction());
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
